package com.redteam.xomtro.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.redteam.xomtro.MasterActivity;
import com.redteam.xomtro.R;
import com.redteam.xomtro.classes.Support;

import java.util.ArrayList;
import java.util.List;

public class ProfileTab_Image extends PlaceTabFragment {
    private float numOfColumns = 2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View r = inflater.inflate(R.layout.fragment_profile_room, container, false);
        assert r != null;
        if (Support.isTablet(activity)) {
            numOfColumns = 3;
        }
        ArrayList<String> eventList = new ArrayList<String>();
        eventList.add("http://lorempixel.com/480/320/nightlife/");
        eventList.add("http://lorempixel.com/480/320/transport/");
        eventList.add("http://lorempixel.com/480/320/food/");
        eventList.add("http://lorempixel.com/480/320/transport/");
        eventList.add("http://lorempixel.com/480/320/sports/");
        eventList.add("http://lorempixel.com/480/320/cats/");
        GridView grid_view = (GridView) r.findViewById(R.id.grid_view);
        float rowCount = (float) Math.ceil(eventList.size() / numOfColumns);
        int totalHeight = (int) Support.dpToPixel((rowCount * (Support.getScaledDensity(activity) * 100f)), activity);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, totalHeight);
        grid_view.setLayoutParams(layoutParams);
        grid_view.setNumColumns((int) numOfColumns);
        ImageListAdapter adapter = new ImageListAdapter(eventList);
        grid_view.setAdapter(adapter);
        return r;
    }

    class ImageListAdapter extends BaseAdapter {

        private final List<String> imageList;

        public ImageListAdapter(List<String> imageList) {
            this.imageList = imageList;
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            ImageView imageView = new ImageView(activity);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) Support.dpToPixel(150f, activity));
            imageView.setLayoutParams(layoutParams);
            imageLoader.displayImage(imageList.get(position), imageView, options);
            return imageView;
        }

        @Override
        public int getCount() {
            return imageList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }

    public ProfileTab_Image newInstance(MasterActivity mainActivity) {
        super.newInstance(mainActivity);
        return this;
    }
}
